package com.uphone.kingmall.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.china.zhuoyue.R;
import com.uphone.kingmall.listener.OnItemClickListener3;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author : SuMeng
 * e-mail : 986335838@qq.com
 * time   : 2019/02/26
 * desc   : 限时购列表页顶部tab
 */
public class XianShiTabAdapter extends BaseQuickAdapter<Object, BaseViewHolder> {
    private final RecyclerView mRecyclerView;
    private final LinearLayoutManager mLinearLayoutManager;
    private Map<Integer, Boolean> integerBooleanMap;
    private OnItemClickListener3 listener;

    public XianShiTabAdapter(RecyclerView recyclerView, LinearLayoutManager linearLayoutManager) {
        super(R.layout.kingmall_item_xianshigou_tab);
        this.mRecyclerView = recyclerView;
        this.mLinearLayoutManager = linearLayoutManager;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final Object item) {
        final int currentPosition = helper.getLayoutPosition();
        helper.setText(R.id.tv_time, currentPosition + ":00");
        Date myDate = new Date();
        int hours = myDate.getHours();
        if (hours < currentPosition) {
            helper.setText(R.id.tv_zhuangtai, "即将开场");
        } else if (hours >= currentPosition && hours < currentPosition + 1) {
            helper.setText(R.id.tv_zhuangtai, "抢购进行中");
        } else {
            helper.setText(R.id.tv_zhuangtai, "已开抢");
        }
        ImageView iv_unchecked = helper.getView(R.id.iv_unchecked);
        ImageView iv_checked = helper.getView(R.id.iv_checked);
        TextView tv_zhuangtai = helper.getView(R.id.tv_zhuangtai);
        if (integerBooleanMap.get(helper.getLayoutPosition())) {
            iv_checked.setVisibility(View.VISIBLE);
            iv_unchecked.setVisibility(View.GONE);
            tv_zhuangtai.setTextColor(mContext.getResources().getColor(R.color.colorWhite));
        } else {
            iv_checked.setVisibility(View.GONE);
            iv_unchecked.setVisibility(View.VISIBLE);
            tv_zhuangtai.setTextColor(mContext.getResources().getColor(R.color.color_tab));
        }

        helper.getView(R.id.rl_item).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setItemClick(currentPosition);
                if (listener != null) {
                    listener.onItemClick(XianShiTabAdapter.this, v, currentPosition);
                }
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener3 listener) {
        this.listener = listener;
    }

    @Override
    public void setOnItemClick(View v, int position) {
        super.setOnItemClick(v, position);
//        setItemClick(position);
    }


    public void setItemClick(int position) {
        for (Map.Entry<Integer, Boolean> entry : integerBooleanMap.entrySet()) {
            if (entry.getKey() == position) {
                integerBooleanMap.put(entry.getKey(), true);
            } else {
                integerBooleanMap.put(entry.getKey(), false);
            }
        }
        notifyDataSetChanged();
        if (position < 0 || position >= getItemCount()) {
            return;
        }
        mRecyclerView.stopScroll();

        int firstItem = mLinearLayoutManager.findFirstVisibleItemPosition();

        int lastItem = mLinearLayoutManager.findLastVisibleItemPosition();
        if (position <= firstItem) {
            mRecyclerView.smoothScrollToPosition(position);
        } else if (position <= lastItem) {
            int top = mRecyclerView.getChildAt(position - firstItem).getTop();
            mRecyclerView.smoothScrollBy(0, top);
        } else {
            mRecyclerView.smoothScrollToPosition(position);
        }
    }

    @Override
    public void setNewData(@Nullable List<Object> data) {
        integerBooleanMap = new HashMap<>();
        for (int i = 0; i < data.size(); i++) {
            if (i == 0) {
                integerBooleanMap.put(i, true);
            } else {
                integerBooleanMap.put(i, false);
            }
        }
        super.setNewData(data);
    }
}